package com.fw.ui.view.common;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.util.comparator.TableHeaderComparator;
import com.fw.ui.util.comparator.TableItemSorter;
import com.fw.ui.view.AbstractPanel;
import com.fw.ui.view.common.PagedTable.PageChangeListener;
import com.fw.ui.view.common.PagedTable.PagedTableChangeEvent;
import com.fw.ui.view.common.util.PagedTableContainer;
import com.fw.ui.view.common.util.TableHeaderProperty;
import com.fw.ui.view.event.AbstractPageTableButtonClickListener;
import com.fw.ui.view.event.AbstractSizeTableValueChangeListener;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnGenerator;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

@Component
@Scope(SCOPE_PROTOTYPE)
@SuppressWarnings({ "all" })
public class TablePanel extends AbstractPanel {

    /**
     * 
     */
    private static final long serialVersionUID = -4931438198031051650L;

    public static final String COLUMN_SEQ_NO = "seqNo";

    public PagedTable table;
    private TextField textFieldPageInput;
    private SortingTablePanel sortingTablePanel;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;

    private BeanItemContainer tableBeanData;
    private IndexedContainer tableIndexedData;

    private String[] columnHeader;
    private String[] columnIdentity;
    private List<String> columnCollapseds;
    private Map<String, TableHeaderProperty> tableHeaderProperties;

    private List<Object> allProperties;
    private Label footerLabel;
    private Locale localeSorter;

    public static int RECORD_LIMIT;
    public static int RECORD_SIZE;

    private boolean isLimitRecord = false;
    private boolean allowSortRecord = true;

    @Override
    public void init() {
	String limitRecord = app.getMessage("TABLE.PANEL.LIMITRECORD");
	try {
	    TablePanel.RECORD_LIMIT = Integer.parseInt(limitRecord);
	} catch (Exception e) {
	    TablePanel.RECORD_LIMIT = 1000;
	    e.printStackTrace();
	}

	String sizeRecord = app.getMessage("TABLE.PANEL.PAGESIZE");
	try {
	    TablePanel.RECORD_SIZE = Integer.parseInt(sizeRecord);
	} catch (Exception e) {
	    TablePanel.RECORD_SIZE = 30;
	    e.printStackTrace();
	}

	this.localeSorter = new Locale("en");

	VerticalLayout layout = new VerticalLayout();
	layout.setSizeFull();

	this.footerLabel = new Label("",ContentMode.HTML);
	this.table = new PagedTable() {

	    /**
	     * 
	     */
	    private static final long serialVersionUID = -7931528006479436657L;

	    @Override
	    protected String formatPropertyValue(Object rowId, Object colId, Property property) {
		// Format by property type
		if (property.getType() == Calendar.class) {
		    if (property.getValue() != null) {
			String pettern = "yyyy/MM/dd HH:mm:ss";
			if (tableHeaderProperties != null) {
			    TableHeaderProperty tableHeaderProperty = tableHeaderProperties.get(colId);
			    if (tableHeaderProperty != null && tableHeaderProperty.getFormatPattern() != null) {
				pettern = tableHeaderProperty.getFormatPattern();
			    }
			}
			SimpleDateFormat df = new SimpleDateFormat(pettern);
			return df.format(((Calendar) property.getValue()).getTime());
		    }
		} else if (property.getType() == Date.class) {
		    if (property.getValue() != null) {
			String pettern = "yyyy/MM/dd HH:mm:ss";
			if (tableHeaderProperties != null) {
			    TableHeaderProperty tableHeaderProperty = tableHeaderProperties.get(colId);
			    if (tableHeaderProperty != null && tableHeaderProperty.getFormatPattern() != null) {
				pettern = tableHeaderProperty.getFormatPattern();
			    }
			}
			SimpleDateFormat df = new SimpleDateFormat(pettern);
			return df.format(property.getValue());
		    }
		} else if (property.getType() == Integer.class) {
		    if (property.getValue() != null) {
			String pettern = "#,###,##0";
			if (tableHeaderProperties != null) {
			    TableHeaderProperty tableHeaderProperty = tableHeaderProperties.get(colId);
			    if (tableHeaderProperty != null && tableHeaderProperty.getFormatPattern() != null) {
				pettern = tableHeaderProperty.getFormatPattern();
			    }
			}
			NumberFormat nf = new DecimalFormat(pettern);
			return nf.format(property.getValue());
		    }
		} else if (property.getType() == Double.class) {
		    if (property.getValue() != null) {
			String pettern = "#,###,##0.00";
			if (tableHeaderProperties != null) {
			    TableHeaderProperty tableHeaderProperty = tableHeaderProperties.get(colId);
			    if (tableHeaderProperty != null && tableHeaderProperty.getFormatPattern() != null) {
				pettern = tableHeaderProperty.getFormatPattern();
			    }
			}
			NumberFormat nf = new DecimalFormat(pettern);
			return nf.format(property.getValue());
		    }
		}
		return super.formatPropertyValue(rowId, colId, property);
	    }
	};

	this.table.setCacheRate(0.1);
	this.table.setSizeFull();
	this.table.setSelectable(false);
	this.table.setMultiSelect(false);
	this.table.setImmediate(true);
	this.table.setColumnCollapsingAllowed(false);
	this.table.setPageLength(TablePanel.RECORD_SIZE);

	this.table.addStyleName("small strong");

	this.footerLabel.addStyleName("custom-table-footer-label");
	this.columnCollapseds = new ArrayList<String>();

	layout.addComponent(this.table);
	layout.setComponentAlignment(this.table, Alignment.MIDDLE_CENTER);

	this.table.setPageLength(1);

	this.setContent(layout);
    }

    public void setPageTable(int page) {
	if (this.textFieldPageInput != null) {
	    this.textFieldPageInput.setValue(String.valueOf(page));
	}
	if (!this.table.getItemIds().isEmpty()) {
	    this.table.setCurrentPage(page);
	}
    }

    public void setManualPageTable(int page) {
	this.table.setManualCurrentPage(page);
    }

    public void disableControlTable() {
	HorizontalLayout controlTableLayout = (HorizontalLayout) ((VerticalLayout) this.getContent()).getComponent(2);
	((VerticalLayout) this.getContent()).removeComponent(controlTableLayout);
    }

    public void createControlTable() {
	HorizontalLayout controlTableLayout = this.table.createControls();
	HorizontalLayout pageManageLayout = (HorizontalLayout) controlTableLayout.getComponent(1);
	pageManageLayout.removeComponent(pageManageLayout.getComponent(3));

	textFieldPageInput = new TextField();
	textFieldPageInput.setValue(String.valueOf(this.table.getCurrentPage()));
	textFieldPageInput.setStyleName(Reindeer.TEXTFIELD_SMALL);
	textFieldPageInput.setImmediate(true);
	textFieldPageInput.setWidth("40px");

	pageManageLayout.addComponent(textFieldPageInput, 3);

	textFieldPageInput.setMaxLength(1);
	textFieldPageInput.addValueChangeListener(new ValueChangeListener() {

	    /**
	     * 
	     */
	    private static final long serialVersionUID = 3256523802681694922L;

	    @Override
	    public void valueChange(ValueChangeEvent event) {
		String value = (String) event.getProperty().getValue();
		try {
		    int inputPage = Integer.parseInt(value);
		    int totalPage = table.getTotalAmountOfPages();
		    String totalPageStr = String.valueOf(totalPage);
		    if (value.trim().length() > totalPageStr.length()) {
			textFieldPageInput.setValue(String.valueOf(totalPage));
		    } else if (inputPage > totalPage) {
			textFieldPageInput.setValue(String.valueOf(totalPage));
		    } else {
			textFieldPageInput.setValue(String.valueOf(inputPage));
		    }
		    table.setCurrentPage(Integer.parseInt(textFieldPageInput.getValue()));
		} catch (Exception e) {
		    textFieldPageInput.setValue(String.valueOf(1));
		    table.setCurrentPage(Integer.parseInt(textFieldPageInput.getValue()));
		}
	    }
	});

	this.table.addListener(new PageChangeListener() {

	    @Override
	    public void pageChanged(PagedTableChangeEvent event) {
		try {
		    int inputPage = event.getCurrentPage();
		    int totalPage = event.getTotalAmountOfPages();
		    if (inputPage > totalPage) {
			textFieldPageInput.setValue(String.valueOf(totalPage));
		    } else {
			textFieldPageInput.setValue(String.valueOf(inputPage));
		    }
		} catch (Exception e) {
		}
	    }
	});

	VerticalLayout layout = (VerticalLayout) this.getContent();

	layout.addComponent(this.footerLabel);

	layout.addComponent(controlTableLayout);

	VerticalLayout verticalLayout = new VerticalLayout();
	verticalLayout.setSizeUndefined();
	verticalLayout.setWidth("100%");
	verticalLayout.setHeight("5px");
	verticalLayout.addStyleName("table-footer-seperate");
	layout.addComponent(verticalLayout);

	layout.setComponentAlignment(controlTableLayout, Alignment.MIDDLE_LEFT);
    }

    public void createControlTablePageNumber() {
	String pageSizeStr = this.propertyBAO.getPropertyValue("com.fw.ui.view.common.TablePanel.pagesize");
	if (pageSizeStr != null) {
	    String[] pageSizeArr = pageSizeStr.split(",");
	    if (pageSizeArr != null) {
		this.table.initPageSize(pageSizeArr);
	    }
	}

	VerticalLayout controlTableLayout = this.table.createControlsPageNumber();
	HorizontalLayout horizontalLayout = (HorizontalLayout) controlTableLayout.getComponent(0);
	horizontalLayout.addComponent(footerLabel, 0);
	horizontalLayout.setComponentAlignment(footerLabel, Alignment.TOP_LEFT);

	VerticalLayout layout = (VerticalLayout) this.getContent();
	layout.addComponent(controlTableLayout);

	VerticalLayout verticalLayout = new VerticalLayout();
	verticalLayout.setSizeUndefined();
	verticalLayout.setWidth("100%");
	verticalLayout.setHeight("5px");
	verticalLayout.addStyleName("table-footer-seperate");
	layout.addComponent(verticalLayout);

	layout.setComponentAlignment(controlTableLayout, Alignment.MIDDLE_LEFT);
    }

    public void setRowIndexVisible() {
	this.table.addGeneratedColumn("seqNo", new ColumnGenerator() {

	    /**
	     * 
	     */
	    private static final long serialVersionUID = 6585654484671417280L;

	    @Override
	    public Object generateCell(Table source, Object itemId, Object columnId) {
		try {
		    PagedTableContainer pagedTableContainer = (PagedTableContainer) table.getContainerDataSource();
		    int index = pagedTableContainer.indexOfId(itemId);
		    return new Integer(index + 1);
		} catch (Exception e) {
		    return new Integer(0);
		}
	    }
	});
    }

    public void setRowIndexVisiblePageNumber() {
	this.table.addGeneratedColumn("seqNo", new ColumnGenerator() {

	    /**
	     * 
	     */
	    private static final long serialVersionUID = 6585654484671417280L;

	    @Override
	    public Object generateCell(Table source, Object itemId, Object columnId) {
		try {
		    PagedTableContainer pagedTableContainer = (PagedTableContainer) table.getContainerDataSource();
		    int index = pagedTableContainer.indexOfId(itemId);
		    int page = table.getManualCurrentPage() - 1;
		    if (page < 0) {
			page = 0;
		    }
		    int rowNo = page * Integer.parseInt((String) table.getManualItemsPerPageSelect().getValue());
		    return new Integer(rowNo + index + 1);
		} catch (Exception e) {
		    return new Integer(0);
		}
	    }
	});
    }

    public boolean clearTableData() {
	try {
	    this.footerLabel.setValue("");
	    this.columnCollapseds.clear();
	    if (this.columnIdentity != null) {
		for (String column : this.columnIdentity) {
		    if (this.table.isColumnCollapsed(column)) {
			this.columnCollapseds.add(column);
		    }
		}
	    }
	    if (this.tableBeanData != null) {
		this.tableBeanData.removeAllItems();
	    }
	    if (this.tableIndexedData != null) {
		this.tableIndexedData.removeAllItems();
	    }
	    this.table.removeAllItems();
	    return true;
	} catch (Exception e) {
	    e.printStackTrace();
	    return false;
	}
    }

    public void setDisableLimitRecord() {
	this.isLimitRecord = false;
    }

    public void setEnableLimitRecord() {
	this.isLimitRecord = true;
    }

    public void setTableSelect(boolean selcted) {
	this.table.setSelectable(selcted);
    }

    public void setTableMultiSelect(boolean selcted) {
	this.table.setMultiSelect(selcted);
    }

    public void setTablePageLength(int length) {
	if (length == 0) {
	    length = this.table.getPageLength();
	}
	this.table.setPageLength(length);
    }

    public void setTableWidth(String width) {
	this.table.setWidth(width);
    }

    public void setTableHeight(String height) {
	this.table.setHeight(height);
    }

    public void setEnableTableSorting(boolean use) {
	this.table.setSortEnabled(use);
    }

    public void setTableColumnCollapsingAllowed(boolean use) {
	this.table.setColumnCollapsingAllowed(use);
    }

    public void setTableColumnCollaps(List<String> columns) {
	for (String column : columns) {
	    this.table.setColumnCollapsed(column, true);
	}
    }

    public void setTableColumnReordering(boolean use) {
	this.table.setColumnReorderingAllowed(use);
    }

    public void setTableColumnHeader(String columnId, String headerTitle) {
	if (this.columnIdentity != null && this.columnIdentity.length > 0) {
	    for (int loop = 0; loop < this.columnIdentity.length; loop++) {
		if (columnIdentity[loop].equals(columnId)) {
		    this.columnHeader[loop] = headerTitle;
		    break;
		}
	    }
	    this.table.setColumnHeader(columnId, headerTitle);
	}
    }

    public void setTableIndexedContainerColumnHeader(List<TableHeaderProperty> headers) {
	this.tableHeaderProperties = new HashMap<String, TableHeaderProperty>();
	this.tableIndexedData = new IndexedContainer();
	this.allProperties = new ArrayList<Object>();
	if (headers != null && !headers.isEmpty()) {
	    Collections.sort(headers, new TableHeaderComparator());
	    int colLength = 0;
	    for (int loop = 0; loop < headers.size(); loop++) {
		TableHeaderProperty th = headers.get(loop);
		if (th.getVisible() != null && th.getVisible().booleanValue()) {
		    colLength++;
		}
	    }
	    this.columnHeader = new String[colLength];
	    this.columnIdentity = new String[colLength];
	    int index = 0;
	    for (int loop = 0; loop < headers.size(); loop++) {
		TableHeaderProperty th = headers.get(loop);
		if (th.getVisible() != null && th.getVisible().booleanValue()) {
		    this.columnHeader[index] = th.getColumnName();
		    this.columnIdentity[index] = th.getColumnId();
		    this.tableHeaderProperties.put(th.getColumnId(), th);
		    index++;
		}
		this.allProperties.add(th.getColumnId());

		this.table.addContainerProperty(th.getColumnId(), th.getType(), th.getDefaultValue(), th.getColumnName(),
			th.getIconResource(), th.getAlignment());
		if (th.getWidth() != null && th.getWidth().intValue() > 0) {
		    this.table.setColumnWidth(th.getColumnId(), th.getWidth());
		} else {
		    this.table.setColumnExpandRatio(th.getColumnId(), 1);
		}

		this.tableIndexedData.addContainerProperty(th.getColumnId(), th.getType(), th.getDefaultValue());

	    }

	    this.table.setVisibleColumns(this.columnIdentity);
	    this.table.setColumnHeaders(this.columnHeader);
	    this.table.setColumnReorderingAllowed(true);
	}
	this.tableIndexedData.setItemSorter(TableItemSorter.getTableItemSorter(this.localeSorter));
    }

    public void setTableBeanContainerColumnHeader(List<TableHeaderProperty> headers, Class c) {
	this.tableHeaderProperties = new HashMap<String, TableHeaderProperty>();
	this.tableBeanData = new BeanItemContainer(c);
	if (headers != null && !headers.isEmpty()) {
	    Collections.sort(headers, new TableHeaderComparator());
	    int colLength = 0;
	    for (int loop = 0; loop < headers.size(); loop++) {
		TableHeaderProperty th = headers.get(loop);
		if (th.getVisible() != null && th.getVisible().booleanValue()) {
		    colLength++;
		}
	    }
	    this.columnHeader = new String[colLength];
	    this.columnIdentity = new String[colLength];
	    int index = 0;
	    for (int loop = 0; loop < headers.size(); loop++) {
		TableHeaderProperty th = headers.get(loop);
		if (th.getVisible() != null && th.getVisible().booleanValue()) {
		    this.columnHeader[index] = th.getColumnName();
		    this.columnIdentity[index] = th.getColumnId();
		    this.tableHeaderProperties.put(th.getColumnId(), th);
		    index++;
		}
		this.table.addContainerProperty(th.getColumnId(), th.getType(), th.getDefaultValue(), th.getColumnName(),
			th.getIconResource(), th.getAlignment());

		if (th.getWidth() != null && th.getWidth().intValue() > 0) {
		    this.table.setColumnWidth(th.getColumnId(), th.getWidth());
		} else {
		    this.table.setColumnExpandRatio(th.getColumnId(), 1);
		}
	    }
	    this.table.setVisibleColumns(this.columnIdentity);
	    this.table.setColumnHeaders(this.columnHeader);
	    this.table.setColumnReorderingAllowed(true);
	}
	this.tableBeanData.setItemSorter(TableItemSorter.getTableItemSorter(this.localeSorter));
    }

    public void setTableIndexedContainerData(List<Map<String, Object>> rowDataList) {
	int currentPage = table.getCurrentPage();
	Object sortId = this.table.getSortContainerPropertyId();
	boolean sortAscending = this.table.isSortAscending();
	this.columnCollapseds.clear();
	for (String column : this.columnIdentity) {
	    if (this.table.isColumnCollapsed(column)) {
		this.columnCollapseds.add(column);
	    }
	}
	this.tableIndexedData.removeAllItems();
	this.table.removeAllItems();
	this.footerLabel.setValue("");
	if (rowDataList != null && !rowDataList.isEmpty()) {
	    int count = 0;
	    for (Map<String, Object> itemMap : rowDataList) {
		Object id = this.tableIndexedData.addItem();
		for (int loop = 0; loop < this.allProperties.size(); loop++) {
		    this.tableIndexedData.getContainerProperty(id, this.allProperties.get(loop)).setValue(
			    itemMap.get(this.allProperties.get(loop)));
		}
		count++;
		if (this.isLimitRecord && count == TablePanel.RECORD_LIMIT) {
		    break;
		}
	    }
	    this.table.setContainerDataSource(this.tableIndexedData);
	    this.footerLabel.setContentMode(ContentMode.HTML);
	}
	this.table.setVisibleColumns(this.columnIdentity);
	this.table.setColumnHeaders(this.columnHeader);
	for (String column : this.columnCollapseds) {
	    this.table.setColumnCollapsed(column, true);
	}
	if (this.allowSortRecord) {
	    this.table.setSortContainerPropertyId(sortId);
	    this.table.setSortAscending(sortAscending);
	    this.table.sort();
	}
	// this.allowSortRecord = true;

	int pageLength = this.table.getPageLength();
	Integer selectPagelength = Integer.parseInt((String) this.table.getItemsPerPageSelect().getValue());
	if (pageLength == 1) {
	    pageLength = selectPagelength;
	}
	if (rowDataList.size() > pageLength) {
	    this.table.setPageLength(pageLength);
	    try {
		String totalPageStr = String.valueOf(this.table.getTotalAmountOfPages());
		this.textFieldPageInput.setMaxLength(totalPageStr.length());
	    } catch (Exception e) {
		this.textFieldPageInput.setMaxLength(1);
	    }
	    this.textFieldPageInput.setValue(String.valueOf(currentPage));
	    this.table.setCurrentPage(Integer.parseInt(textFieldPageInput.getValue()));
	} else {
	    if (rowDataList.isEmpty() || rowDataList.size() == 0) {
		this.table.setPageLength(1);
	    } else {
		this.table.setPageLength(rowDataList.size());
	    }
	}

    }

    public void setTableBeanContainerData(List<BeanItem> dataList) {
	int currentPage = table.getCurrentPage();
	Object sortId = this.table.getSortContainerPropertyId();
	boolean sortAscending = this.table.isSortAscending();
	this.columnCollapseds.clear();
	for (String column : this.columnIdentity) {
	    if (this.table.isColumnCollapsed(column)) {
		this.columnCollapseds.add(column);
	    }
	}
	this.tableBeanData.removeAllItems();
	this.table.removeAllItems();
	this.footerLabel.setValue("");
	if (dataList != null && !dataList.isEmpty()) {
	    int count = 0;
	    for (BeanItem item : dataList) {
		this.tableBeanData.addBean(item.getBean());
		count++;
		if (this.isLimitRecord && count == TablePanel.RECORD_LIMIT) {
		    break;
		}
	    }
	    this.table.setContainerDataSource(this.tableBeanData);
	    this.footerLabel.setContentMode(ContentMode.HTML);
	}
	this.table.setVisibleColumns(this.columnIdentity);
	this.table.setColumnHeaders(this.columnHeader);
	for (String column : this.columnCollapseds) {
	    this.table.setColumnCollapsed(column, true);
	}
	if (this.allowSortRecord) {
	    this.table.setSortContainerPropertyId(sortId);
	    this.table.setSortAscending(sortAscending);
	    this.table.sort();
	}
	// this.allowSortRecord = true;

	int pageLength = this.table.getPageLength();
	if (this.table.getItemsPerPageSelect() != null && this.table.getItemsPerPageSelect().getValue() != null) {
	    Integer selectPagelength = Integer.parseInt((String) this.table.getItemsPerPageSelect().getValue());
	    if (pageLength == 1) {
		pageLength = selectPagelength;
	    }
	    if (dataList.size() > pageLength) {
		this.table.setPageLength(pageLength);
		try {
		    String totalPageStr = String.valueOf(this.table.getTotalAmountOfPages());
		    this.textFieldPageInput.setMaxLength(totalPageStr.length());
		} catch (Exception e) {
		    this.textFieldPageInput.setMaxLength(1);
		}
		this.textFieldPageInput.setValue(String.valueOf(currentPage));
		this.table.setCurrentPage(Integer.parseInt(textFieldPageInput.getValue()));
	    } else {
		if (dataList.isEmpty() || dataList.size() == 0) {
		    this.table.setPageLength(1);
		} else {
		    this.table.setPageLength(dataList.size());
		}
	    }
	} else {
	    if (dataList.isEmpty() || dataList.size() == 0) {
		this.table.setPageLength(1);
	    } else {
		this.table.setPageLength(dataList.size());
	    }
	}
    }

    public void setTableIndexedContainerDataTotalPage(List<Map<String, Object>> rowDataList, Integer totalPage, Long totalElement) {
	Object sortId = this.table.getSortContainerPropertyId();
	boolean sortAscending = this.table.isSortAscending();
	this.columnCollapseds.clear();
	for (String column : this.columnIdentity) {
	    if (this.table.isColumnCollapsed(column)) {
		this.columnCollapseds.add(column);
	    }
	}
	this.tableIndexedData.removeAllItems();
	this.table.removeAllItems();
	this.footerLabel.setValue("");
	if (rowDataList != null && !rowDataList.isEmpty()) {
	    int count = 0;
	    for (Map<String, Object> itemMap : rowDataList) {
		Object id = this.tableIndexedData.addItem();
		for (int loop = 0; loop < this.allProperties.size(); loop++) {
		    this.tableIndexedData.getContainerProperty(id, this.allProperties.get(loop)).setValue(
			    itemMap.get(this.allProperties.get(loop)));
		}
		count++;
		if (this.isLimitRecord && count == TablePanel.RECORD_LIMIT) {
		    break;
		}
	    }
	    this.table.setContainerDataSource(this.tableIndexedData);
	    this.footerLabel.setContentMode(ContentMode.HTML);
	}

	String footerStr = this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.label.tablefooter");
	this.footerLabel.setValue(totalElement + footerStr);

	this.table.setVisibleColumns(this.columnIdentity);
	this.table.setColumnHeaders(this.columnHeader);
	for (String column : this.columnCollapseds) {
	    this.table.setColumnCollapsed(column, true);
	}
	if (this.allowSortRecord) {
	    this.table.setSortContainerPropertyId(sortId);
	    this.table.setSortAscending(sortAscending);
	    this.table.sort();
	}

	int pageLength = this.table.getPageLength();
	Integer selectPagelength = Integer.parseInt((String) this.table.getManualItemsPerPageSelect().getValue());
	if (!this.table.getPageSizes().contains(new Integer(pageLength))) {
	    pageLength = selectPagelength;
	}
	if (rowDataList.size() > pageLength) {
	    this.table.setPageLength(pageLength);
	} else {
	    if (rowDataList.isEmpty() || rowDataList.size() == 0) {
		this.table.setPageLength(1);
	    } else {
		this.table.setPageLength(rowDataList.size());
	    }
	}

	this.table.setManualTotalPage(totalPage);
	this.table.setManualTotalElement(totalElement);
	this.table.updatePaging();
    }

    public void setTableBeanContainerDataTotalPage(List<BeanItem> dataList, Integer totalPage, Long totalElement) {
	Object sortId = this.table.getSortContainerPropertyId();
	boolean sortAscending = this.table.isSortAscending();
	this.columnCollapseds.clear();
	for (String column : this.columnIdentity) {
	    if (this.table.isColumnCollapsed(column)) {
		this.columnCollapseds.add(column);
	    }
	}
	this.tableBeanData.removeAllItems();
	this.table.removeAllItems();
	this.footerLabel.setValue("");
	if (dataList != null && !dataList.isEmpty()) {
	    int count = 0;
	    for (BeanItem item : dataList) {
		this.tableBeanData.addBean(item.getBean());
		count++;
		if (this.isLimitRecord && count == TablePanel.RECORD_LIMIT) {
		    break;
		}
	    }
	    this.table.setContainerDataSource(this.tableBeanData);
	    this.footerLabel.setContentMode(ContentMode.HTML);
	}

	String footerStr = this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.label.tablefooter");
	this.footerLabel.setValue(totalElement + footerStr);

	this.table.setVisibleColumns(this.columnIdentity);
	this.table.setColumnHeaders(this.columnHeader);
	for (String column : this.columnCollapseds) {
	    this.table.setColumnCollapsed(column, true);
	}
	if (this.allowSortRecord) {
	    this.table.setSortContainerPropertyId(sortId);
	    this.table.setSortAscending(sortAscending);
	    this.table.sort();
	}

	int pageLength = this.table.getPageLength();
	Integer selectPagelength = Integer.parseInt((String) this.table.getManualItemsPerPageSelect().getValue());
	if (!this.table.getPageSizes().contains(new Integer(pageLength))) {
	    pageLength = selectPagelength;
	}
	if (dataList.size() > pageLength) {
	    this.table.setPageLength(pageLength);
	} else {
	    if (dataList.isEmpty() || dataList.size() == 0) {
		this.table.setPageLength(1);
	    } else {
		this.table.setPageLength(dataList.size());
	    }
	}

	this.table.setManualTotalPage(totalPage);
	this.table.setManualTotalElement(totalElement);
	this.table.updatePaging();

    }

    public void setAllowSortRecord(boolean allowSortRecord) {
	this.allowSortRecord = allowSortRecord;
    }

    public PagedTable getTable() {
	return this.table;
    }

    public BeanItemContainer getTableBeanItemContainer() {
	return this.tableBeanData;
    }

    public IndexedContainer getTableIndexedContainer() {
	return this.tableIndexedData;
    }

    @Override
    public void initListener() {
	// TODO Auto-generated method stub

    }

    @Override
    public void initValidator() {
	// TODO Auto-generated method stub

    }

    public Locale getLocaleSorter() {
	return localeSorter;
    }

    public void setLocaleSorter(Locale localeSorter) {
	this.localeSorter = localeSorter;
    }

    public Integer getManualCurrentPage() {
	return this.table.getManualCurrentPage();
    }

    public Integer getPageSize() {
	Integer selectPagelength = Integer.parseInt((String) this.table.getItemsPerPageSelect().getValue());
	return selectPagelength;
    }

    public Integer getManualPageSize() {
	Integer selectPagelength = Integer.parseInt((String) this.table.getManualItemsPerPageSelect().getValue());
	return selectPagelength;
    }

    public void addPageTableButtonClickListener(AbstractPageTableButtonClickListener pageTableButtonClickListener) {
	this.table.addPageTableButtonClickListener(pageTableButtonClickListener);
    }

    public void addSizeTableValueChangeListener(AbstractSizeTableValueChangeListener sizeTableValueChangeListener) {
	this.table.addSizeTableValueChangeListener(sizeTableValueChangeListener);
    }

    public void setSortingTablePanel(SortingTablePanel sortingTablePanel) {
	this.sortingTablePanel = sortingTablePanel;
    }

    public SortingTablePanel getSortingTablePanel() {
	return sortingTablePanel;
    }

    @Override
    public void initData() {
	// TODO Auto-generated method stub

    }

    @Override
    public void updateStrings() {
	this.table.getpFirst().setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.button.first.caption"));
	this.table.getpLast().setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.button.last.caption"));
	this.table.getpNext().setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.button.next.caption"));
	this.table.getpPrevious().setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.button.previous.caption"));

	this.table.getItemsPerPageLabel1().setValue(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.label.display.title1"));
	this.table.getItemsPerPageLabel2().setValue(this.messageBAO.getMessage("com.fw.ui.view.common.TablePanel.label.display.title2"));
    }

}
